/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package classesDAO;

import classes.Contato;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;




public class DAOContato {

    private Connection con;

    public void gravar(Contato contato) {

        try {

            con = (Connection) Conexao.getConnection();

            PreparedStatement ps = null;
            ps = (PreparedStatement) con.prepareStatement("insert into contato (nome,telefone) values(?,?)");
            ps.setString(1, contato.getNome().toUpperCase());
            ps.setString(2, contato.getTelefone());
            ps.execute();
            ps.close();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(DAOContato.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void alterar(Contato contato) {
        try {
            con = (Connection) Conexao.getConnection();
            PreparedStatement ps = null;
            ps = (PreparedStatement) con.prepareStatement("update contato set nome=?,telefone=?"+
                                "where idcontato=?");
            ps.setString(1, contato.getNome().toUpperCase());
            ps.setString(2, contato.getTelefone());
            ps.setInt(3, contato.getIdContato());
            ps.execute();
            ps.close();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(DAOContato.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void excluir(Contato contato) {
         try {
            con =(Connection) Conexao.getConnection();
            PreparedStatement ps = null;
            ps = (PreparedStatement) con.prepareStatement("delete from contato where idcontato=?");
            ps.setInt(1, contato.getIdContato());
            ps.execute();
            ps.close();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(DAOContato.class.getName()).log(Level.SEVERE, null, ex);
        }


    }


    // usar parametro da url
    public void excluir(int id) {
         try {
            con =  (Connection) Conexao.getConnection();
            PreparedStatement ps = null;
            ps = (PreparedStatement) con.prepareStatement("delete from contato where idcontato=?");
            ps.setInt(1, id);
            ps.execute();
            ps.close();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(DAOContato.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public ArrayList<Contato> getTodosContatos() {
        ArrayList<Contato>array = new  ArrayList<Contato>();
        try {
            con = (Connection) Conexao.getConnection();
            Statement smt= (Statement) con.createStatement();//cria o a variavel para executar o comando
            ResultSet rs = smt.executeQuery("select idcontato, nome, telefone from contato");
            while(rs.next()){
                Contato c= new Contato();
                c.setIdContato(rs.getInt("idcontato"));
                c.setNome(rs.getString("nome"));
                c.setTelefone(rs.getString("telefone"));
                array.add(c);
            }
            rs.close();
            con.close();

        } catch (SQLException ex) {
            Logger.getLogger(DAOContato.class.getName()).log(Level.SEVERE, null, ex);
        }

        return array;
    }

    public Contato getContatoPorId(int id) {
        Contato c= new Contato();
         try {
            con = (Connection) Conexao.getConnection();
            PreparedStatement ps= (PreparedStatement)
            con.prepareStatement("select idcontato, nome, telefone from contato where idcontato=?");
            ps.setInt(1, id);
            ResultSet rs= ps.executeQuery();
            rs.next();
            c.setIdContato(rs.getInt("idcontato"));
            c.setNome(rs.getString("nome"));
            c.setTelefone(rs.getString("telefone"));
            rs.close();
            ps.close();
            con.close();

        } catch (SQLException ex) {
            Logger.getLogger(DAOContato.class.getName()).log(Level.SEVERE, null, ex);
        }
        return c;
    }

    public ArrayList<Contato> getContatosPorNome(String nome) {
        return null;


    }
}
